<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8">
      <el-card>
        <div class="user">
          <img :src="userImg" alt="">
          <div class="user-info">
            <p class="name" style="font-size: 1.5rem; font-weight: bold">Admin</P>
            <p class="access" style="font-size: 0.8rem;">超级管理员1</P>
          </div>
        </div>
        <div class="login-info">
          <p>上次登陆的事件： <span>2022-8-1</span></p>
          <p>上次登陆的地点： <span>成都</span></p>
        </div>
      </el-card>
      <el-card style="height:460px; margin-top: 20px; overflow: auto;">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column v-for="(item, index) in tableLabel" :key="index" :prop="index" :label="item">
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col style="margin-top: 20px" :span="16">
      <div class="num">
        <el-card v-for="item in countData" :key="item.name" :body-style="{ display: 'flex', padding: 0 }">
          <i class="icon" :class="`el-icon-${item.icon}`" :style="{ background: item.color }"></i>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="txt">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <el-card style="height: 280px">
        <div style="height: 280px" ref="echarts">
        </div>
      </el-card>
      <div class="graph">
        <el-card style="height: 260px">
          <div style="height: 240px" ref="userEcharts">
          </div>
        </el-card>
        <el-card style="height: 260px">
          <div style="height: 240px" ref="vedioEcharts">
          </div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { getData } from '../../../api/data.js'
import * as echarts from 'echarts'

export default {
  data() {
    return {
      userImg: require('../../assets/images/user.png'),
      tableData: [],
      // tableData: [
      //   {
      //     name: 'oppo',
      //     todayBuy: 100,
      //     monthBuy: 300,
      //     totalBuy: 800
      //   },
      //   {
      //     name: 'vivo',
      //     todayBuy: 100,
      //     monthBuy: 300,
      //     totalBuy: 800
      //   },
      //   {
      //     name: '苹果',
      //     todayBuy: 100,
      //     monthBuy: 300,
      //     totalBuy: 800
      //   },
      //   {
      //     name: '小米',
      //     todayBuy: 100,
      //     monthBuy: 300,
      //     totalBuy: 800
      //   },
      //   {
      //     name: '三星',
      //     todayBuy: 100,
      //     monthBuy: 300,
      //     totalBuy: 800
      //   },
      //   {
      //     name: '魅族',
      //     todayBuy: 100,
      //     monthBuy: 300,
      //     totalBuy: 800
      //   }
      // ],
      tableLabel: {
        name: '课程',
        todayBuy: '今日购买',
        monthBuy: '本月购买',
        totalBuy: '总购买'
      },
      countData: [
        {
          name: "今日支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "今日未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "本月未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
      ],
    }
  },
  mounted() {
    getData().then(res => {
      const { code, data } = res.data
      if (code === 20000) {
        this.tableData = data.tableData
        const order = data.orderData
        const keyArray = Object.keys(order.data[0])
        const series = []
        keyArray.forEach(key => {
          series.push({
            name: key,
            data: order.data.map(item => item[key]),
            type: 'line'
          })
        })
        const option = {
            xAxis: {
              data: order.date
            },
            yAxis: {},
            legend: {
              data: keyArray
            },
            series
          }

          const E = echarts.init(this.$refs.echarts)
          E.setOption(option)

          // 柱状图，用户图
          const userOption = {
            legend: {
              // 图例文字颜色
              textStyle: {
                color: "#333",
              },
            },
            grid: {
              left: "20%",
            },
            // 提示框
            tooltip: {
              trigger: "axis",
            },
            xAxis: {
              type: "category", // 类目轴
              data: data.userData.map(item => item.date),
              axisLine: {
                lineStyle: {
                  color: "#17b3a3",
                },
              },
              axisLabel: {
                interval: 0,
                color: "#333",
              },
            },
            yAxis: [
              {
                type: "value",
                axisLine: {
                  lineStyle: {
                    color: "#17b3a3",
                  },
                },
              },
            ],
            color: ["#2ec7c9", "#b6a2de"],
            series: [
              {
                name:'新增用户',
                data: data.userData.map(item => item.new),
                type:'bar'
              },
              {
                name:'活跃用户',
                data: data.userData.map(item => item.active),
                type:'bar'
              }
            ],
          }
          const User = echarts.init(this.$refs.userEcharts)
          User.setOption(userOption)

          // 饼状图
          const vedioOption = {
          tooltip: {
            trigger: "item",
          },
          color: [
            "#0f78f4",
            "#dd536b",
            "#9462e5",
            "#a6a6a6",
            "#e1bb22",
            "#39c362",
            "#3ed1cf",
          ],
          
          series: [
            {
              data: data.videoData,
              type: 'pie'
            }
          ],
        }
        const Vedio = echarts.init(this.$refs.vedioEcharts)
        Vedio.setOption(vedioOption)
      }
      console.log(res);
    })
  }
}
</script>

<style lang="less" scoped>
// .el-row {

//     .user {
//       display: flex;
//       justify-content: flex-start;
//       align-items: center;

//       .user-info {
//         margin-left: 20px;
//       }
//     }

//     .login-info {
//       display: flex;
//       flex-direction: column;
//       justify-content: space-around;
//       align-items: left;
//       font-size: 0.8rem;
//     }
//   }
</style>